<?php
namespace frontend\controllers;

use common\models\UserCategory;
use common\WuMing;
use Yii;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use common\models\Post;
use yii\data\Pagination;
use yii\filters\AccessControl;
use common\models\Category;


/**
 * Logincontroller
 */
class FoundController extends Controller
{


    public function actionIndex($type = null)
    {



        $query = Post::find()->joinWith('cate');

        if ($type == "new") {
            $post = $query->orderBy(['post.id' => SORT_DESC])->asArray()->where(['post.status' => 1,'post.sign'=>1]);
            $pages = new Pagination([
                'totalCount' => $post->count(),
                'defaultPageSize' => 10
            ]);
            $model = $post->offset($pages->offset)->limit($pages->limit)->all();
        } elseif ($type == null) {
            $sql ="SELECT *,post.view*10+post.zan*3+post.cai num FROM post WHERE post.status =1 ORDER BY num DESC";
            $q = Yii::$app->db->createCommand($sql)->queryAll();
            $pages = new Pagination([
                'totalCount' => count($q),
                'defaultPageSize' => 20
            ]);
            $model = Yii::$app->db->createCommand($sql." limit ".$pages->limit." offset ".$pages->offset."")->queryAll();

            $temp = [];
            foreach($model as $v){
                $temp1 = $v;
                $temp2['cate'] = Category::findOne(['id'=>$v['cate_id']])->toArray();
                $temp[] = array_merge($temp1,$temp2);
            }

            $model = $temp;
        } else {
            throw new BadRequestHttpException("参数错误！");
        }

        //将结果传递到视图
        //   WuMing::dd($model);die;

        $title = WuMing::getSiteInfo("site-name")->value;
        $keywords = WuMing::getSiteInfo("site-keywords")->value;
        $description = WuMing::getSiteInfo("site-description")->value;
        return $this->render('index', [
            'model' => $model,
            'pages' => $pages,
            'type' => $type,
            'title' => $title,
            'keywords' => $keywords,
            'description' => $description,
        ]);
    }



}
